package com.memogame;

import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class HallofFameActivity extends Activity {

	private static String METHOD_NAME = "GetHallOfFame";
	private static String SOAP_ACTION = Data.NAMESPACE + METHOD_NAME;
	AsyncCallWS task;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		task = new AsyncCallWS();
		task.execute();
		
		setContentView(R.layout.activity_hallof_fame);
	}
	
	
	public void on2Click(View v){
		TextView text = (TextView) findViewById(R.id.hofTextView);
		text.setText(task.getList2());
		//Log.i(Data.TAG,"z1 2click");
	}
	
	public void on4Click(View v){
		TextView text = (TextView) findViewById(R.id.hofTextView);
		text.setText(task.getList4());
		//Log.i(Data.TAG,"z1 4click");
	}

	public void on6Click(View v){
		TextView text = (TextView) findViewById(R.id.hofTextView);
		text.setText(task.getList6());
		//Log.i(Data.TAG,"z1 6click");
	}
	
	public void on8Click(View v){
		TextView text = (TextView) findViewById(R.id.hofTextView);
		text.setText(task.getList8());
		//Log.i(Data.TAG,"z1 8click");
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_hallof_fame, menu);
		return true;
	}

	private class AsyncCallWS extends AsyncTask<Void, Void, Void> {

		ArrayList<HOFItem> list2 = new ArrayList<HOFItem>();
		ArrayList<HOFItem> list4 = new ArrayList<HOFItem>();
		ArrayList<HOFItem> list6 = new ArrayList<HOFItem>();
		ArrayList<HOFItem> list8 = new ArrayList<HOFItem>();
		//TextView text;
		
		public AsyncCallWS(){
			//this.text = text;
		}

		@Override
		protected Void doInBackground(Void... arg0) {
			
			SoapObject Request = new SoapObject(Data.NAMESPACE, METHOD_NAME);

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.dotNet = true;
			envelope.setOutputSoapObject(Request);

			try {
				AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
						Data.URL);

				androidHttpTransport.call(SOAP_ACTION, envelope);

				SoapObject result = (SoapObject) envelope.bodyIn;
				SoapObject root = (SoapObject) result.getProperty(0); //.
				SoapObject element1 = (SoapObject) root.getProperty(0); //<hof>
								
				int i = 0;
				while (i < element1.getPropertyCount()) {
					SoapObject element2 = (SoapObject) element1.getProperty(i); //<items>
					int itemsLength = element2.getPropertyCount();
					//Log.i(Data.TAG, "z1: ----------------");
					
					String playerName, time;
					
					switch(i){
					case 0:
						for(int j=0; j<itemsLength; j++) {
							SoapObject element3 = (SoapObject) element2.getProperty(j); //<item>
							playerName = element3.getProperty(0).toString();
							time = element3.getProperty(1).toString();
							//Log.i(Data.TAG, "z1: " + playerName + " " + time);
							list2.add(new HOFItem((j+1), playerName, time));
						}
						break;
					case 1:
						for(int j=0; j<itemsLength; j++) {
							SoapObject element3 = (SoapObject) element2.getProperty(j); //<item>
							playerName = element3.getProperty(0).toString();
							time = element3.getProperty(1).toString();
							//Log.i(Data.TAG, "z1: " + playerName + " " + time);
							list4.add(new HOFItem((j+1), playerName, time));
						}
						break;
					case 2:
						for(int j=0; j<itemsLength; j++) {
							SoapObject element3 = (SoapObject) element2.getProperty(j); //<item>
							playerName = element3.getProperty(0).toString();
							time = element3.getProperty(1).toString();
							//Log.i(Data.TAG, "z1: " + playerName + " " + time);
							list6.add(new HOFItem((j+1), playerName, time));
						}
						break;
					case 3:
						for(int j=0; j<itemsLength; j++) {
							SoapObject element3 = (SoapObject) element2.getProperty(j); //<item>
							playerName = element3.getProperty(0).toString();
							time = element3.getProperty(1).toString();
							//Log.i(Data.TAG, "z1: " + playerName + " " + time);
							list8.add(new HOFItem((j+1), playerName, time));
						}
						break;
						
					default:
						break;
					}
										
					i++;
				}

			} catch (Exception e) {
				Log.e(Data.TAG, "z1 error-hof: " + e.getMessage());
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
//			Log.i(Data.TAG, "z1 " + list2.toString());
//			Log.i(Data.TAG, "z1 " + list4.toString());
//			Log.i(Data.TAG, "z1 " + list6.toString());
//			Log.i(Data.TAG, "z1 " + list8.toString());
		}
		
		protected String listToString(ArrayList<HOFItem> l) {
			String s = "";
			for(int k=0; k<l.size(); k++){
				s+=l.get(k).toString();
			}
			
			if(s.equals("")){
				s = "No results found.";
			}
			
			return s;
		}
		
		public String getList2(){
			return listToString(list2);
		}
		
		public String getList4(){
			return listToString(list4);
		}
		
		public String getList6(){
			return listToString(list6);
		}
		
		public String getList8(){
			return listToString(list8);
		}
		


	}
	
}
